//
//  FR_Record_InformationViewController.m
//  FaceRecognition
//
//  Created by Sylar on 2016/12/6.
//  Copyright © 2016年 Sylar. All rights reserved.
//

#import "FR_Record_InformationViewController.h"

@interface FR_Record_InformationViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    int lastType ;
}

@property (nonatomic, strong) UITableView *recordTable;

@end

@implementation FR_Record_InformationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navBar.nav_titleLabel.text = @"记录详情";
    
    lastType = [SYAccountTool ShareAccount].account.lasttype;
    
    [self.view addSubview:self.recordTable];
    
    if (_infoID > 0) {
        
        if (lastType == 1) {
            
            [self requestTeacherInfoData];
        }else
        {
            [self requestStudentInfoData];
        }
    }
    
    
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - Table 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.recordType == 0?6:5;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row < 2) {
        return 76;
    }else {
        return 44;
    }
    
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UILabel *titleLabel;
    UILabel *contentLabel;
    UIImageView *headImageView;
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CellIdentifier"];
    
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"CellIdentifier"];
    }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(15, 75.5, SCREENWIDTH, 0.5)];
        [lineView setBackgroundColor:colorFromHex(@"#DEDEDE")];
        if (indexPath.row < 2) {
            
            titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(15, 30, 100, 16)];
            [titleLabel setFont:[UIFont systemFontOfSize:16]];
            [titleLabel setTextColor:colorFromHex(@"#979797")];
            [cell.contentView addSubview:titleLabel];
            
            headImageView = [[UIImageView alloc]initWithFrame:CGRectMake(SCREENWIDTH - 75, 8, 60, 60)];
            [cell.contentView addSubview:headImageView];
            
            if (indexPath.row == 0) {
                titleLabel.text = @"识别照片";
                [headImageView sd_setImageWithURL:[NSURL URLWithString:self.recordModel.userphoto]];
            }else if (indexPath.row == 1) {
                titleLabel.text = @"抓拍照片";
                [headImageView sd_setImageWithURL:[NSURL URLWithString:self.recordModel.photo]];
            }
            
            [cell.contentView addSubview:lineView];
            
            return cell;
            
        }else {
            
            [lineView setY:43.5];
            if (self.recordType == 0) {
                if (indexPath.row == 5) {
                    [lineView setX:0];
                }
            }else {
                if (indexPath.row == 4) {
                    [lineView setX:0];
                }
            }
            
            titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(15, 14, 100, 16)];
            [titleLabel setFont:[UIFont systemFontOfSize:16]];
            [titleLabel setTextColor:colorFromHex(@"#979797")];
            [cell.contentView addSubview:titleLabel];
            
            contentLabel = [[UILabel alloc]initWithFrame:CGRectMake(SCREENWIDTH - 215, 14, 200, 16)];
            [contentLabel setFont:[UIFont systemFontOfSize:16]];
            [contentLabel setTextColor:colorFromHex(@"#3b3b3b")];
            contentLabel.textAlignment = NSTextAlignmentRight;
            [cell.contentView addSubview:contentLabel];
            
            if (indexPath.row == 2) {
                titleLabel.text = @"姓名";
                contentLabel.text = self.recordModel.username;
            }else if (indexPath.row == 3) {
                titleLabel.text = @"学校";
                contentLabel.text = self.recordModel.scname;
                
            }else if (indexPath.row == 4) {
                if (self.recordType == 0) {
                    titleLabel.text = @"班级";
                    contentLabel.text = self.recordModel.classname;
                }else {
                    titleLabel.text = @"时间";
                    
                    if (self.recordModel.zptime.length == 19) {
                        NSString *dateStr = [self.recordModel.zptime substringToIndex:self.recordModel.zptime.length-3];
                        [contentLabel setText:[NSString stringWithFormat:@"%@(%@)",dateStr,self.recordModel.inorout]];
                    }
                }
            }else if (indexPath.row == 5) {
                titleLabel.text = @"时间";
                
                if (self.recordModel.zptime.length == 19) {
                    NSString *dateStr = [self.recordModel.zptime substringToIndex:self.recordModel.zptime.length-3];
                    
                    [contentLabel setText:[NSString stringWithFormat:@"%@(%@)",dateStr,self.recordModel.inorout]];
                }
                
            }
        
        [cell.contentView addSubview:lineView];
    
    
         return cell;
    }
}




-(void)requestTeacherInfoData {
    
    NSMutableDictionary *paramDic = [[NSMutableDictionary alloc]init];
    [paramDic setObject:_infoID forKey:@"infoid"];
    if (self.schoolID) {
        [paramDic setObject:self.schoolID forKey:@"scid"];
    }else {
        [paramDic setObject:[NSString stringWithFormat:@"%ld",(long)self.recordModel.scid] forKey:@"scid"];
    }
    [paramDic setObject:USERID forKey:@"userid"];
    
    [Utils Java_get:@"api/getEmployeeEventDetail" parameters:paramDic success:^(NSInteger recode, id jsonObject) {

        
        NSDictionary * dic = [jsonObject objectForKey:@"re_data"];
        
        if (dic.count > 0) {
            self.recordModel = [FR_HistoryRecord_Model mj_objectWithKeyValues:dic];
        }
        
        
        [_recordTable reloadData];
        
    } failure:^(NSInteger recode, NSError *error) {
        [self showHint:error.localizedDescription];
    }];

}
-(void)requestStudentInfoData
{
    
    NSMutableDictionary *paramDic = [[NSMutableDictionary alloc]init];
    [paramDic setObject:_infoID forKey:@"infoid"];
    if (self.classID) {
        [paramDic setObject:self.classID forKey:@"classid"];
    }else {
        [paramDic setObject:[NSString stringWithFormat:@"%ld",(long)self.recordModel.classid] forKey:@"classid"];
    }
    [paramDic setObject:[SYAccountTool ShareAccount].account.scid forKey:@"scid"];
    [paramDic setObject:USERID forKey:@"userid"];
    
    [Utils Java_get:@"api/getStuEventParDetail" parameters:paramDic success:^(NSInteger recode, id jsonObject) {
        
        NSDictionary * dic = [jsonObject objectForKey:@"re_data"];
        
        self.recordModel = [FR_HistoryRecord_Model mj_objectWithKeyValues:dic];
        
        [_recordTable reloadData];
        
    } failure:^(NSInteger recode, NSError *error) {
        [self showHint:error.localizedDescription];
    }];
    
}

#pragma mark - Get

- (UITableView *)recordTable {
    
    if (_recordTable == nil) {
        
        _recordTable = [[UITableView alloc]initWithFrame:CGRectMake(0, SafeAreaTopHeight, SCREENWIDTH, 0) style:(UITableViewStylePlain)];
        [_recordTable setHeight:self.recordType == 0 ?328:284];
        _recordTable.delegate = self;
        _recordTable.dataSource = self;
        _recordTable.separatorStyle = UITableViewCellSeparatorStyleNone;
        _recordTable.backgroundColor = colorFromHex(@"#F5F8FA");
        _recordTable.scrollEnabled = NO;
        
    }
    return _recordTable;
}

@end
